package com.echo.week;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author echo
 * @Description countdownLatch 计数门闩案例
 * @Date 2023/10/20.
 * @Version 1.0
 */
public class DemoCountdownLatch {

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <=6;i++){
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"\t上完班，离开公司");
                countDownLatch.countDown();  //减少countDownLatch 的数量
            },String.valueOf(i)).start();
        }

        new Thread(()->{
            try {
                countDownLatch.await(); //当countDownLatch 的数量为零时，唤起线程
                System.out.println(Thread.currentThread().getName()+"\t他最后才离开公司");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"7").start();

    }
}
